package com.dzt.androidframework.fragment;

import android.view.View;
import android.widget.CompoundButton;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivitySetting;
import com.dzt.androidframework.app.LocalApplication;
import com.dzt.androidframework.utils.JLogKit;
import com.dzt.androidframework.widgets.SettingItemView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.fragment_setting_main)
public class FragmentSettingMain extends FragmentBase {
	@ViewInject(R.id.item_update_pwd)
	private SettingItemView itemUpdatePwd;
	@ViewInject(R.id.item_auto_record)
	private SettingItemView itemAutoRecord;
	@ViewInject(R.id.item_update)
	private SettingItemView itemUpdate;
	@ViewInject(R.id.item_about)
	private SettingItemView itemAbout;

	@Override
	protected void initWidgets() {
		boolean isRecord = LocalApplication.getInstance().sharedPreferences.getBoolValue("auto_record", false);
		itemAutoRecord.setToggleChecked(isRecord);
		itemAutoRecord.setToggleBtnVisible(autoRecordChangedListener);
	}

	@Event(value = {R.id.item_update_pwd, R.id.item_auto_record,
			R.id.item_update, R.id.item_about})
	private void onViewClick(View view) {
		ActivitySetting activity = (ActivitySetting) getActivity();
		switch (view.getId()) {
			case R.id.item_update_pwd:
				activity.showFragmentByIndex(ActivitySetting.INDEX_SETTING_PWD);
				break;
			case R.id.item_auto_record:
				itemAutoRecord.changeToggleBtnCheck();
				break;
			case R.id.item_update:
				activity.showFragmentByIndex(ActivitySetting.INDEX_SETTING_UPDATE);
				break;
			case R.id.item_about:
				activity.showFragmentByIndex(ActivitySetting.INDEX_SETTING_ABOUT);
				break;
		}
	}

	private CompoundButton.OnCheckedChangeListener autoRecordChangedListener = new CompoundButton.OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			JLogKit.getInstance().i("onCheckedChanged isChecked = " + isChecked);
			LocalApplication.getInstance().sharedPreferences.setBoolValue("auto_record", isChecked);
		}
	};
}
